package lpl.com.xyd.test.manage.factory;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.factory.AbstractDomainFactory;
import lpl.com.xyd.test.manage.common.factory.AutowiredDomainFactory;
import lpl.com.xyd.test.manage.dao.ProjectDemandsMapperDAO;
import lpl.com.xyd.test.manage.domain.ProjectDemandsDomain;
import lpl.com.xyd.test.manage.dto.ProjectDemandsDTO;
import lpl.com.xyd.test.manage.param.ProjectDemandsParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProjectDemandsDomainFactory extends AbstractDomainFactory<ProjectDemandsDomain, ProjectDemandsParam> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectDemandsDomainFactory.class);

    @Autowired
    private AutowiredDomainFactory autowiredDomainFactory;

    @Autowired
    private ProjectDemandsMapperDAO projectDemandsMapperDAO;


    @Override
    public ProjectDemandsDomain create(ProjectDemandsParam param) {
        LOGGER.info("创建领域，domain:{}", JSONObject.toJSONString(param));
        ProjectDemandsDomain domain = autowiredDomainFactory.create(ProjectDemandsDomain.class);
        BeanUtils.copyProperties(param,domain);
        return domain;
    }

    @Override
    public ProjectDemandsDomain active(String domainId) {
        return null;
    }

    @Override
    public List<ProjectDemandsDomain> activeList(ProjectDemandsParam param) {
        List<ProjectDemandsDTO> projectDemandsDTOS = projectDemandsMapperDAO.selectByParam(param);
        if (null == projectDemandsDTOS || projectDemandsDTOS.size() <= 0){
            return null;
        }

        List<ProjectDemandsDomain> domains = new ArrayList<>(projectDemandsDTOS.size());
        projectDemandsDTOS.forEach(v->{
            ProjectDemandsDomain domain = new ProjectDemandsDomain();
            BeanUtils.copyProperties(v,domain);
            domains.add(domain);
        });
        return domains;
    }

    @Override
    public Boolean store(ProjectDemandsDomain domain) {
        LOGGER.info("保存领域，domain:{}", JSONObject.toJSONString(domain));
        ProjectDemandsDTO projectDemandsDTO = new ProjectDemandsDTO();
        BeanUtils.copyProperties(domain,projectDemandsDTO);
        int result = projectDemandsMapperDAO.insert(projectDemandsDTO);
        return result > 0;
    }

    @Override
    public Boolean reStore(ProjectDemandsDomain domain) {
        return null;
    }

    @Override
    public Boolean reStore(List<ProjectDemandsDomain> domains) {
        return null;
    }

    @Override
    public boolean delete(Long domainId) {
        return false;
    }

    @Override
    public boolean delete(List<Long> domainId) {
        return false;
    }
}
